//
//  ReadBookVOrH_VC+PopWords.m
//  KidReading
//
//  Created by telen on 16/3/8.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ReadBookVOrH_VC+PopWords.h"
#import "ConstDefine.h"

@interface ReadBookVOrH_VC()
@property(nonatomic,strong)WordsView* wordsView;
@end

@implementation ReadBookVOrH_VC (PopWords)

ASSOCIATED(wordsView, setWordsView, WordsView*, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

+ (void)load
{
    [self swizzleSelector:@selector(viewDidLoad) withSelector:@selector(viewDidLoad_wordsview)];
    [self swizzleSelector:@selector(readViewDidScrolledTo:) withSelector:@selector(readViewDidScrolledTo_wordsview:)];
    [self swizzleSelector:@selector(applicationDidEnterBackground:) withSelector:@selector(applicationDidEnterBackground_wordsview:)];
    [self swizzleSelector:@selector(readViewDidTapWord:) withSelector:@selector(readViewDidTapWord_wordsview:)];
}

- (void)viewDidLoad_wordsview
{
    [self viewDidLoad_wordsview];
    //
    //
    [mutex addMutexView:self.wordsView];
}

- (void)applicationDidEnterBackground_wordsview:(UIApplication *)application
{
    [self applicationDidEnterBackground_wordsview:application];
    //
    [self.wordsView unShow_must];
}

- (void)readViewDidTapWord_wordsview:(KoK_RectView *)rectview
{
    [self readViewDidTapWord_wordsview:rectview];
    //
    StatusButton_2_View* pWordsModelBtn = [self msgTo_menthed:@"pWordsModelBtn" refs:nil needReturnValue:YES];
    //
    if ([self.readSource isEqualToString:ReadSource_Pep] && pWordsModelBtn.onOff == NO) {
        return;
    }
    //
    if (self.isAutoRead) {
        [NSObject msgToTarget:self menthed:@"didTapSelfView:" refs:nil needReturnValue:NO];
        return;
    }
    //
    if (!rectview.keyWordPinyin || [rectview.keyWordPinyin isEqualToString:@""]) {
        return;
    }
    //
    NSString* py = rectview.keyWordPinyin;
    NSString* newPy = py;

    if (self.wordsView.hidden) {
        self.wordsView.pt_unshow = rectview.center;
        self.wordsView.pt_unshowFollowView = rectview;
        [self.wordsView setKeyWords:rectview.keyWord andPy_number:newPy];
        [self.wordsView addWaittingView];
        [self.wordsView show];
    }else{
        [self.wordsView clean];
        self.wordsView.pt_unshow = rectview.center;
        self.wordsView.pt_unshowFollowView = rectview;
        [self.wordsView setKeyWords:rectview.keyWord andPy_number:newPy];
        [self.wordsView addWaittingView];
    }
    //
    self.wordsView.enBook_Dic = readView.currentPageView.bookType;
    //
    [[WordsDictData shareSingle] getwordsInfo_bookID:self.bookid bookOid:self.bookOid words:rectview.keyWord pinYin:newPy phrase:rectview.phrase phraseIdx:rectview.phrease_idx isEnglishWord:readView.currentPageView.bookType];
    //
    [NSObject msgToTarget:self menthed:@"statistical_getword:" refs:@[rectview.keyWord] needReturnValue:NO];
}

- (void)readViewDidScrolledTo_wordsview:(PageView *)pageView
{
    [self readViewDidScrolledTo_wordsview:pageView];
    //
    [DataProcess do_Main_quick:^{
        self.wordsView.currentPage_C = self.currentIndex;
    }];
}


- (void)addWordsView
{
    CGFloat words_w = 500 / ratio_w;
    CGFloat words_h = 700 / ratio_w;
//    if ([DataProcessing shareSingle].deviceType == DevicesTypes_iPhone) {
//        words_w *= 1.25;
//        words_h *= 1.25;
//    }
    CGFloat w = width_my;
    CGFloat h = height_my;
    CGPoint pt_show = CGPointMake(w*1/2, h*1/2);
    CGPoint pt_unshow = pt_show;
    //
    if (!self.wordsView) {
        self.wordsView = [[WordsView alloc] initWithFrame:CGRectMake(0, 0, words_w, words_h)];
        [self.view addSubview:self.wordsView];
        self.wordsView.center = pt_show;
    }
    self.wordsView.pt_show = pt_show;
    self.wordsView.pt_unshow = pt_unshow;
    self.wordsView.pt_unshowFollowView = nil;
    self.wordsView.transform = CGAffineTransformMakeScale(0, 0);
    self.wordsView.hidden = YES;
    self.wordsView.bookName = self.bookName;
    self.wordsView.currentBookId = self.bookid;
    [DataProcess do_Main_quick:^{
        self.wordsView.currentPage_C = self.currentIndex;
    }];
}

- (NSNumber*)didTapSelfView_unShowWordsView
{
    if (self.wordsView && !self.wordsView.hidden) {
        [self.wordsView unShow];
        return @(YES);
    }
    return nil;
}

- (NSNumber*)isWordsViewShowing
{
    if(self.wordsView.isInShowing){
        return @(YES);
    }
    return nil;
}

- (NSNumber*)isWordsViewWorking
{
    if(self.wordsView && (self.wordsView.hidden == NO || self.wordsView.isInShowing)){
        return @(YES);
    }
    return nil;
}

- (void)wordsViewMustUnshow
{
    [self.wordsView unShow_must];
}


//MARK: WordsDict
- (void)wordsDictData_DidGetInfo:(NSDictionary *)infoDict forWords:(NSString *)words andPinyin:(NSString *)py
{
    [self.wordsView updateWordsWithDict:infoDict forWords:words andPinyin:py];
}

- (void)wordsDictData_DidGetSound_forWords:(NSString *)words andPinyin:(NSString *)py
{
    [self.wordsView setSoundReady_forWords:words andPinyin:py];
    //    NSLog(@"%@",words);
}

@end
